﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using OpenNETCF.Drawing.Imaging;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmCamera :FrmBase
    {
        private LinkFile linkFile;
        private string parentForm = "";
        private LinkFileService linkFileService = new LinkFileService();
        private bool needSave = true ;

        public FrmCamera()
        {
            InitializeComponent();
            ttlbrhead.Text = "照片浏览";
            ilstbcup.Images.Add(ResourceImage.s_拍摄01);
            ilstbcup.Images.Add(ResourceImage.s_确定01);
            ilstbcup.Images.Add(ResourceImage.s_查看01);
            ilstbcup.Images.Add(ResourceImage.s_返回01);
            ilstdown.Images.Add(ResourceImage.s_拍摄02);
            ilstdown.Images.Add(ResourceImage.s_确定02);
            ilstdown.Images.Add(ResourceImage.s_查看02);
            ilstdown.Images.Add(ResourceImage.s_返回02);
            ilstdisable.Images.Add(ResourceImage.conew_s_拍摄01);
            ilstdisable.Images.Add(ResourceImage.conew_s_确定01);
            ilstdisable.Images.Add(ResourceImage.conew_s_查看01);
            ilstdisable.Images.Add(ResourceImage.conew_s_返回01);
        }

        private void FrmCamera_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbPictureTitle.Text  = linkFile.Title;

            //显示图片
            try
            {
                ImagingFactory factory = new ImagingFactoryClass();
                IImage img;
                factory.CreateImageFromFile(linkFile.FullFileName, out img);
                IBitmapImage imgB;
                factory.CreateBitmapFromImage(img,
                                              (uint)pbImage.Width,
                                              (uint)pbImage.Height,
                                              System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                                              InterpolationHint.InterpolationHintDefault,
                                              out imgB);
                pbImage.Image = ImageUtils.IBitmapImageToBitmap(imgB);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("图片装载错误!");
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentForm = (string)Parameter[0];
            linkFile = (LinkFile)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            parentForm = (string)Caches[0];
            linkFile = (LinkFile)Caches[1];

            needSave = false;
        }

        private void bcxunjian_OnButtonClick_1(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:
                    ContinueTakePicture();
                    break;
                case 1:  //确认
                    if(SavePhoto())
                        mssdlgexec.ShowDialog(this, "照片已保存！", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case 2: //查看
                    ViewPhotos();
                    break;
                case 3:
                    FormManage.GotoNext(parentForm, null,null, true);
                    break;
            }
        }

        /// <summary>
        /// 继续拍摄
        /// </summary>
        private void ContinueTakePicture()
        {
            linkFile = linkFileService.TaskPicture(linkFile.LinkID);
            needSave = true;
        }

        private bool SavePhoto()
        {
            if (needSave)
            {
                linkFileService.AddLinkFile(linkFile);
                needSave = false;

                //根据父窗体来确认是否要更改关联文件对应信息的上报状态
                switch (parentForm)
                {
                    case "FrmPatrolException":  //巡视异常信息的照片
                    case "FrmElseWork":         //其他工作的照片
                        PatrolInfoService patrolInfoService = new PatrolInfoService();
                        PatrolDetail patrolDetail = patrolInfoService.GetPatrolDetail(linkFile.LinkID);
                        patrolDetail.ReportState = "未上报";
                        patrolInfoService.UpdatePatrolDetail(patrolDetail);
                        break;
                    default:
                        break;
                }

                return true;
            }
            else
                return false;
        }

        private void ViewPhotos()
        {
            if (needSave)
            {
                //先保存当前图片
                if (mssdlgSavePhoto.ShowDialog(this, "是否保存当前照片？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
                    SavePhoto();
            }

            FormManage.GotoNext("FrmPictureList", new object[] { "FrmCamera", "'" + linkFile.LinkID + "'"}, new object[] { parentForm,linkFile }, false);

        }


    }
}